package com.example.mytips.structure;


import com.example.mytips.ImagePopupPlugin;
import com.example.mytips.assets.FlutterAssetsDialog;
import com.example.mytips.utils.DialogUtils;
import com.example.mytips.utils.Mye;
import com.example.mytips.utils.ShowInFinder;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;

import com.intellij.psi.codeStyle.CodeStyleManager;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NotNull;



/**
 * 自定义保存操作，用来刷新杨昌专用的类和方法，还有定位
 */
public class CustomSaveAction extends AnAction {

    public static FlutterPlusWindow flutterPlusWindow;

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Mye mye = Mye.builder(e);

        Project project = mye.project;
        if (project == null) return;
        if (mye.psiFile == null) return;

        // 在写操作中执行格式化
        WriteCommandAction.runWriteCommandAction(project, () -> {
            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
            codeStyleManager.reformat(mye.psiFile);

            // 执行默认的保存操作
            FileDocumentManager.getInstance().saveAllDocuments();

            if (flutterPlusWindow != null) {
                flutterPlusWindow.outRefreshUI();
            }
        });
    }

}
